#include "networklist.h"
#include <QDebug>

using namespace Moolink;

 QMap<QString, NetWorkThread*> hostItems;

NetWorkList::NetWorkList(QObject *parent)
{
    queryHostInfoInDb();
}
NetWorkList::~NetWorkList()
{

}
void NetWorkList::queryHostInfoInDb()
{

    //sqldate->Query("select * from server");

}

void NetWorkList::getLocalHostInfo()
{
    QString localHostName = QHostInfo::localHostName();
    //qDebug() << "LocalHostName:" << localHostName;


    //获取IP地址
    QHostInfo info = QHostInfo::fromName(localHostName);
    //qDebug() << "IP Address:" << info.addresses();


    foreach(QHostAddress address, info.addresses())
    {
        if (address.protocol() == QAbstractSocket::IPv4Protocol)
            qDebug() << "IPv4 Address:" << address.toString();
    }


    foreach (QHostAddress address, QNetworkInterface::allAddresses())
    {
        qDebug() << "Address:" << address;
    }


    //获取所有网络接口的列表
    foreach (QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
    {
        //设备名
        qDebug() << "Device:" << netInterface.name();


        //MAC地址
        qDebug() << "HardwareAddress:" << netInterface.hardwareAddress();


        QList<QNetworkAddressEntry> entryList = netInterface.addressEntries();


        //遍历每一个IP地址(每个包含一个IP地址，一个子网掩码和一个广播地址)
        foreach(QNetworkAddressEntry entry, entryList)
        {
            //IP地址
            qDebug() << "IP Address:" << entry.ip().toString();


            //子网掩码
            qDebug() << "Netmask:" << entry.netmask().toString();


            //广播地址
            qDebug() << "Broadcast:" << entry.broadcast().toString();
        }
    }

}



void NetWorkList::setCallbackFunc(void(*func) (int hostIndex,QString hostName,QString res,int error))
{
    m_callbackFunc = func;
}


void NetWorkList::showRequest(int hostIndex,const QString &hostName,const QString &res)
{
    //qDebug()<<"NetWorkList:"<<res<<"\r\n";
   m_callbackFunc(hostIndex,hostName,res,0);
}

void NetWorkList::processError(int hostIndex,const QString &hostName,const QString &res)
{
    //qDebug()<<"NetWorkList:"<<res<<"\r\n";
   m_callbackFunc(hostIndex,hostName,res,1);
}




void NetWorkList::writeDataToHost(int hostindex,Host host,int waitMSEC,QString datastr)
{
    writeDataToNetWork(hostindex,host.HostName,host.HostIp,host.HostPort,waitMSEC,datastr);
}

void NetWorkList::writeDataToNetWork(int hostindex,QString hostname,QString hostip,int hostport,int waitMSEC,QString datastr)
{
    QMap<QString, NetWorkThread*>::iterator serialFinder;
    serialFinder = hostItems.find(hostip+":"+QString::number(hostport));
    if(serialFinder != hostItems.end())//如果找到现有的连接就直接发送
    {
        serialFinder.value()->transaction(hostindex,hostname,hostip,hostport,waitMSEC,datastr);
    }
    else//如果没有现有连接就新建后再发送,但首次发送会不成功,待分析原因
    {
        hostItems.insert(hostip+":"+QString::number(hostport), new NetWorkThread());

        serialFinder = hostItems.find(hostip+":"+QString::number(hostport));
        if(serialFinder != hostItems.end())
        {
            connect(serialFinder.value(), &NetWorkThread::response2, this, &NetWorkList::showRequest);
            connect(serialFinder.value(), &NetWorkThread::error, this, &NetWorkList::processError);
//            connect(serialFinder.value(), &NetWorkThread::timeout, this, &NetWorkList::processTimeout);

            serialFinder.value()->transaction(hostindex,hostname,hostip,hostport,waitMSEC,datastr);
        }
    }

}
